package com.example.mcdonaldapplication.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.mcdonaldapplication.databinding.ActivityWalletBinding
import com.example.mcdonaldapplication.info.ServerIP
import com.example.mcdonaldapplication.info.TicketInfo
import com.example.mcdonaldapplication.info.TicketAdapter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import okhttp3.FormBody
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONObject
import kotlin.concurrent.thread


class WalletActivity : AppCompatActivity() {
    private lateinit var binding:ActivityWalletBinding
    private val serverIP = ServerIP.getIp()
    private val ticketList : ArrayList<TicketInfo> = ArrayList()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityWalletBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.hide()

        //跳转充值
        val btnCharge = binding.btnCharge
        btnCharge.setOnClickListener{
            val intent = Intent(this, ChargeActivity::class.java)
            startActivity(intent)
        }

        //跳转支付
        val btnPay = binding.btnPay
        btnPay.setOnClickListener{
            val intent = Intent(this, QRcodeActivity::class.java)
            startActivity(intent)
        }

        getUser()
        getTicket()
    }

    private fun getTicket(){
        thread {
            val prefs = getSharedPreferences("user", Context.MODE_PRIVATE)
            val uid = prefs.getInt("uid", 0)
            //post方式提交的数据
            val formBody: FormBody = FormBody.Builder()
                .add("uid", uid.toString())
                .build()
            val client = OkHttpClient()
            val request = Request.Builder()
                .url("http://${serverIP}/ticket/queryAll")
                .post(formBody)
                .build()
            val response = client.newCall(request).execute()
            val responseData = response.body.string()
            parseJsonTicket(responseData)
        }
    }

    @SuppressLint("SetTextI18n")
    private fun parseJsonTicket(jsonData: String) {
        val gson = Gson()
        val jsonObject = JSONObject(jsonData)
        val message = jsonObject.getString("message")
        val data = jsonObject.getString("data")
        if(message != "优惠券为空"){
            val typeOf = object : TypeToken<List<TicketInfo>>() {}.type
            val ticketInfoList = gson.fromJson<List<TicketInfo>>(data, typeOf)
            for (ticket in ticketInfoList) {
                ticketList.add(ticket)
            }
            Log.d("json",jsonData)
            runOnUiThread{
                val layoutManager = LinearLayoutManager(this)
                binding.TicketRecycleView.layoutManager = layoutManager
                val recycleAdapter = TicketAdapter(ticketList, this)
                binding.TicketRecycleView.adapter = recycleAdapter
                val tvTicketNum = binding.tvTicketNum
                tvTicketNum.text = "优惠券(${recycleAdapter.itemCount})"
            }
        }else{
            binding.tvTicketNum.text = "优惠券(0)"
        }
    }

    private fun getUser(){
        val tvBalance = binding.tvBalance
        val prefs = getSharedPreferences("user", Context.MODE_PRIVATE)
        val balance = prefs.getString("balance", "0.0")
        tvBalance.text = "$balance"
    }

    override fun onResume() {
        super.onResume()
        getUser()
    }
}